/**
 * 
 */
package org.mspring.crm.client.mvc;

import java.util.List;

import org.mspring.crm.client.AppEvents;
import org.mspring.crm.client.frame.FrameFactory;
import org.mspring.crm.client.model.ItemModel;
import org.mspring.crm.client.remote.ItemRemote;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.TabPanelEvent;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Gao Youbo
 * @since 2012-3-5
 * 
 */
public class ContentView extends View {

    private TabPanel tabPanel;

    /**
     * @param controller
     */
    public ContentView(Controller controller) {
        super(controller);
        // TODO Auto-generated constructor stub
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.extjs.gxt.ui.client.mvc.View#initialize()
     */
    @Override
    protected void initialize() {
        // TODO Auto-generated method stub
        tabPanel = new TabPanel();
        tabPanel.setCloseContextMenu(true);
        tabPanel.setBorderStyle(false);
        tabPanel.setBodyBorder(false);
        tabPanel.setTabScroll(true);
        tabPanel.setAnimScroll(true);

        tabPanel.addListener(Events.Remove, new Listener<TabPanelEvent>() {

            public void handleEvent(TabPanelEvent be) {
                // TabItem item = be.getItem();
                // Entry entry = (Entry) item.getData("entry");
                // Dispatcher.forwardEvent(AppEvents.HidePage, entry);
            }

        });
        tabPanel.addListener(Events.Select, new Listener<TabPanelEvent>() {
            public void handleEvent(TabPanelEvent be) {
                // String token = History.getToken();
                // Entry entry = (Entry) be.getItem().getData("entry");
                // if (token != null && (!token.equals(entry.getId()))) {
                // History.newItem(entry.getId(), false);
                // }
                // Dispatcher.forwardEvent(AppEvents.TabChange, entry);
            }
        });
        ContentPanel center = (ContentPanel) Registry.get(ApplicationView.CENTER_PANEL);
        center.add(tabPanel);
        center.layout();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.extjs.gxt.ui.client.mvc.View#handleEvent(com.extjs.gxt.ui.client.
     * mvc.AppEvent)
     */
    @Override
    protected void handleEvent(AppEvent event) {
        // TODO Auto-generated method stub
        EventType type = event.getType();
        if (type == AppEvents.ShowPage) {
            // Entry entry = event.getData();
            ItemModel item = event.getData();
            onShowPage(item);
        }
    }

    public void onShowPage(final ItemModel treeItem) {
        ItemRemote.Util.getInstance().findPanelItem(treeItem.getId(), new AsyncCallback<List<ItemModel>>() {

            @Override
            public void onSuccess(List<ItemModel> result) {
                // TODO Auto-generated method stub
                

                TabItem tabItem = tabPanel.findItem("page__" + treeItem.getId(), false);
                if (tabItem == null) {
                    tabItem = new TabItem();
                    tabItem.setData("entry", treeItem);
                    tabItem.setClosable(true);
                    tabItem.setId("page__" + treeItem.getId());
                    tabItem.setText(treeItem.getName());
                    tabItem.setLayout(new FitLayout());
                    
                    Widget page = treeItem.get("page");
                    if (page == null) {
                        page = FrameFactory.createFrame(result);
                        treeItem.set("page", page);
                    }
                    tabItem.add(page);
                    tabPanel.add(tabItem);
                }

                if (tabItem != tabPanel.getSelectedItem()) {
                    tabPanel.setSelection(tabItem);
                }
            }

            @Override
            public void onFailure(Throwable caught) {
                // TODO Auto-generated method stub

            }
        });
        // Page page = item.get("page");
        // if (page == null) {
        // page = new Page(item);
        // item.set("page", page);
        // }
        //
        // TabItem tabItem = tabPanel.findItem("page__" + page.getId(), false);
        // if (tabItem == null) {
        // tabItem = new TabItem();
        // tabItem.setData("entry", item);
        // tabItem.setClosable(true);
        // tabItem.setId("page__" + page.getId());
        // tabItem.setText(item.getName());
        // tabItem.setLayout(new FitLayout());
        // tabItem.add(page);
        // tabPanel.add(tabItem);
        // }
        //
        // if (tabItem != tabPanel.getSelectedItem()) {
        // tabPanel.setSelection(tabItem);
        // }
    }

}
